package com.ruoyi.web.controller.note;

import java.util.List;

import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.note.domain.TBookChapter;
import com.ruoyi.note.service.ITBookChapterService;
import com.ruoyi.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 章节管理Controller
 *
 * @author cl
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/note/chapter")
public class TBookChapterController extends BaseController {
    @Autowired
    private ITBookChapterService tBookChapterService;


    @Autowired
    private TokenService tokenService;

    /**
     * 查询章节管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:list')")
    @GetMapping("/list")
    public AjaxResult list(TBookChapter tBookChapter, HttpServletRequest request) {
        tBookChapter.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        List<TBookChapter> list = tBookChapterService.selectTBookChapterList(tBookChapter);
        return AjaxResult.success(list);
    }
    /**
     * 查询章节管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:list')")
    @GetMapping("/getAll")
    public AjaxResult getAll(TBookChapter tBookChapter, HttpServletRequest request) {
        tBookChapter.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        List<TBookChapter> list = tBookChapterService.selectTBookChapterList(tBookChapter);
        return AjaxResult.success(list);
    }

    /**
     * 导出章节管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:export')")
    @Log(title = "章节管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBookChapter tBookChapter, HttpServletRequest request) {
        tBookChapter.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        List<TBookChapter> list = tBookChapterService.selectTBookChapterList(tBookChapter);
        ExcelUtil<TBookChapter> util = new ExcelUtil<TBookChapter>(TBookChapter.class);
        return util.exportExcel(list, "章节管理数据");
    }

    /**
     * 获取章节管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tBookChapterService.selectTBookChapterById(id));
    }

    /**
     * 新增章节管理
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:add')")
    @Log(title = "章节管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBookChapter tBookChapter, HttpServletRequest request) {
        tBookChapter.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tBookChapterService.insertTBookChapter(tBookChapter));
    }

    /**
     * 修改章节管理
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:edit')")
    @Log(title = "章节管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBookChapter tBookChapter, HttpServletRequest request) {
        tBookChapter.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tBookChapterService.updateTBookChapter(tBookChapter));
    }

    /**
     * 删除章节管理
     */
    @PreAuthorize("@ss.hasPermi('note:chapter:remove')")
    @Log(title = "章节管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBookChapterService.deleteTBookChapterByIds(ids));
    }
}
